package org.zcl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.zcl.dao.TbBasicChannelMapper;
import org.zcl.dao.TbChannelCategoryMapper;
import org.zcl.entity.TbBasicChannel;
import org.zcl.entity.TbChannelCategory;
import org.zcl.entity.TbUnionChannel;
import org.zcl.controller.TUnionBasicChannelDeleteVO;
import org.zcl.service.TbBasicChannelService;
import org.springframework.stereotype.Service;
import org.zcl.service.TbChannelCategoryService;
import org.zcl.service.TbUnionChannelService;

/**
 * 频道(TbBasicChannel)表服务实现类
 *
 * @author Chenglin Zhu
 * @since 2022-01-06 16:49:32
 */
@Service("tbBasicChannelService")
@Slf4j
public class TbBasicChannelServiceImpl extends ServiceImpl<TbBasicChannelMapper, TbBasicChannel> implements TbBasicChannelService {

    @Autowired
    private TbChannelCategoryMapper tbChannelCategoryMapper;

    @Autowired
    private TbBasicChannelService tbBasicChannelService;

    @Autowired
    private TbUnionChannelService tbUnionChannelService;

    @Autowired
    private TbChannelCategoryService tbChannelCategoryService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Boolean deleteBasicChannel(TUnionBasicChannelDeleteVO vo) {
        Integer unionId = vo.getUnionId();
        String goodsChannelCode = vo.getGoodsChannelCode();
        // 根据频道找到类目进行删除
        int count = tbChannelCategoryMapper.selectCount(new LambdaQueryWrapper<TbChannelCategory>()
                .eq(TbChannelCategory::getUnionId, unionId)
                .eq(TbChannelCategory::getChannelId, goodsChannelCode));
        try {
            if (count == 0) {
                return deleteBasicChannel(unionId, goodsChannelCode);
            } else {
                // 删除已添加的分类数据
                boolean deleteCategoryResult = tbChannelCategoryService.remove(new LambdaQueryWrapper<TbChannelCategory>()
                        .eq(TbChannelCategory::getUnionId, unionId)
                        .eq(TbChannelCategory::getChannelId, goodsChannelCode));
                if (deleteCategoryResult) {
                    Boolean deleteBasicChannel = deleteBasicChannel(unionId, goodsChannelCode);
                    if (!deleteBasicChannel) {
                        throw new Exception("删除频道数据失败！");
                    }
                    return true;
                } else {
                    throw new Exception("删除已添加的分类数据失败！");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("删除频道时异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Boolean deleteBasicChannel2(TUnionBasicChannelDeleteVO vo) {
        Integer unionId = vo.getUnionId();
        String goodsChannelCode = vo.getGoodsChannelCode();
        Boolean result =  deleteBasicChannel(unionId, goodsChannelCode);
        tbChannelCategoryService.remove(new LambdaQueryWrapper<TbChannelCategory>()
                .eq(TbChannelCategory::getUnionId, unionId)
                .eq(TbChannelCategory::getChannelId, goodsChannelCode));
        int i = 2/0;
        return result;
    }


    private Boolean deleteBasicChannel(Integer unionId, String goodsChannelCode) {
        boolean deleteBasicChannel = tbBasicChannelService.remove(new LambdaQueryWrapper<TbBasicChannel>()
                .eq(TbBasicChannel::getUnionId, unionId)
                .eq(TbBasicChannel::getChannelId, goodsChannelCode));
        if (deleteBasicChannel) {
            return tbUnionChannelService.remove(new LambdaQueryWrapper<TbUnionChannel>()
                    .eq(TbUnionChannel::getUnionId, unionId)
                    .eq(TbUnionChannel::getChannelId, goodsChannelCode));
        }
        return false;
    }
}

